package net.crazyblock.mobilesafe.ui;

import java.security.PublicKey;

import net.crazyblock.mobilesafe.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 自定义的组合控件，用以将相同的条目抽取出来 初始化布局文件
 * 
 * @author ZL
 * 
 */
public class SettingItemView extends RelativeLayout {
	private TextView tv_title;
	private TextView tv_desc;
	private CheckBox cb_status;
	private String desc_on;
	private String desc_off;

	private void iniView(Context context) {
		// TODO Auto-generated method stub
		View.inflate(context, R.layout.setting_item_view, this);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		tv_desc = (TextView) this.findViewById(R.id.tv_desc);
		cb_status = (CheckBox) this.findViewById(R.id.cb_status);
	}

	public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		iniView(context);
	}

	public SettingItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		iniView(context);
		String title = attrs.getAttributeValue("http://schemas.android.com/apk/res/net.crazyblock.mobilesafe", "title");
		desc_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/net.crazyblock.mobilesafe", "desc_on");
		desc_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/net.crazyblock.mobilesafe", "desc_off");
		tv_title.setText(title);
		if (cb_status.isChecked()) {
			tv_desc.setText(desc_on);
		} else {
			tv_desc.setText(desc_off);
		}
	}

	public SettingItemView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		iniView(context);
	}
/**
 * 检查checkbox是否被选中
 * @return 选中状态
 */
	public boolean isCheck() {
		return cb_status.isChecked();

	}
	/**
	 * 设置checkbox的状态
	 * @param checked
	 */
	public void setCheck(boolean checked){
		if (checked) {
			tv_desc.setText(desc_on);
		} else {
			tv_desc.setText(desc_off);
		}
		cb_status.setChecked(checked);
	}
	/**
	 * 设置组合控件条目的描述信息
	 * @param text 描述信息
	 */
	public void setDescription(CharSequence text){
		tv_desc.setText(text);
	};
}
